﻿#if INTERACTIVE
#load "FsBson.fs"
#load "FsMgProtocol.fs"
#load "FsMgDriver.fs"
#else
module BasicTests
#endif

open FsBson
open FsMgDriver
open FsMgProtocol
open System


let a() = findOneC "admin.users" (bson[])
let b() = findOneC "admin.$cmd" (bson ["replSetGetStatus",1; "forShell",1])
let c() = queryC {defQ with Collection="test.system.namespaces"; Query=(bson[]); Take=100}

let bdoc = 
    bson
        [
            "Type",  bval "Book"
            "Title", bval "Definitive Guide to MongoDB,the"
            "ISBN" , bval "987-1-4302-3051-9" 
            "Publisher", bval "Apress"
            "Author", bval ["Membrey, Peter"; "Plugge, Eelco"; "Hawkins, Tim"]
        ]

let bdoc2 = 
    bson
        [
            "Type",  bval "Book"
            "Title", bval "Expert F#"
            "ISBN" , bval "987-1-4302-3051-9" 
            "Publisher", bval "Apress"
            "Author", bval ["Don Syme"]
        ]

let basicDataTypes =
    bson [
        "BStr_Empty", bval ""
        "BDate",     DateTime(2012,12,31) |> bval
        "BTimestamp", Int64.Parse("100") |> bval
        "BFloat", bval 20.
        "BInt", bval 20
        "BInt64", Int64.Parse("200") |> bval
        "BBool", bval true
        "BBinary", bval [|0uy; 12uy; 43uy|]
        "BSym_Empty", BSym ""
        "BDoc_Empty", bson[]
        "BArr_Empty", BArr [||]
        "BRegex", BRegex ("","") 
        "BJS", BJS ""
        "BMinKey", BMinKey
        "BMaxKey", BMaxKey
        "BJSWS", BJSWS ("",bson[])
        ]

let insd() = insertC "test.unit_tests" [bdoc]
let insd2() = insertC "test.unit_tests" [bdoc2]
let insd3() = insertC "test.unit_tests" [basicDataTypes]
let getd3() = queryC {defQ with Collection="test.unit_tests"; Take=1; Query=bson["BDoc_Empty",bson[]] }
let getAll() = queryC {defQ with Collection="test.unit_tests"; Take=100}
let deleteAll() = deleteC "test.unit_tests" (bson[]) DeleteFlags.None

let dbCommandsTests() =
    printfn "Users pass=%b" <| a().IsNone
    printfn "replSetGetStatus pass=%A" <| b().IsSome
    (c().Results |> Seq.length) > 0 |> printfn "namespaces pass=%A"

let dataTests() =
    deleteAll()
    insd()
    insd2()
    insd3()
    (getAll().Results |> Seq.length) > 3 |> printfn "3 inserted pass=%b"
    ((getd3().Results |> Seq.head).Map |> Map.remove "_id") = basicDataTypes.Map |> printfn "basic data types pass=%b"

